//
//  YFStatusViewModel.swift
//  微博
//
//  Created by 余仿 on 16/4/12.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

/// 自身被OriginalView持有,自己持有模型,对数据进行洗白
class YFStatusViewModel: NSObject {

    var statusList:YFStatusList?

    //懒加载,用的时候才加载,并且只会调用一次
    //但这不是懒加载,是计算属性,每次滚动都会加载
    //数据实际上第一次就已经请求下来了
    var headImageURL:NSURL?

    var mbImage:UIImage?

    var verifiedImage:UIImage?


    var retweetTitle:String?

    var commentTitle:String?

    var attitudeTitle:String?

    var time:String?{

        let date =  NSDate.sinaDate(statusList?.created_at ?? "")

//        print("come here")
        //对象方法处理时间
        return date?.fullDescription()

    }


    var source:String?

    init(statusList:YFStatusList) {

        super.init()

        self.statusList = statusList

        headImageURL = NSURL(string: statusList.user?.profile_image_url ?? "")

        clearMbRank()

        clearVerifiedImage()

        retweetTitle = clearBottomBtnTitle(statusList.reposts_count,defaultTitle: "转发")

        commentTitle = clearBottomBtnTitle(statusList.comments_count,defaultTitle: "评论")

        attitudeTitle = clearBottomBtnTitle(statusList.attitudes_count,defaultTitle: "赞")

        clearSource()

    }


    /**
     微博来源处理
     */
    func clearSource(){

        let s = statusList?.source ?? ""

        //Optional("<a href=\"http://app.weibo.com/t/feed/3o33sO\" rel=\"nofollow\">iPhone 6</a>")
        //截取字符串
        let startFlag = "\">"
        let endFlag = "</a>"

        if let startRange = s.rangeOfString(startFlag),let endRange = s.rangeOfString(endFlag){

            let range = startRange.endIndex ..< endRange.startIndex

            source = s.substringWithRange(range)
        }

    }

    /**
     底部条文字处理
     */
    func clearBottomBtnTitle(count:Int,defaultTitle:String) -> String{

        if count > 0 && count < 10000{
            return "\(count)"
        }else if(count >= 10000){
            //如果余数>十分之一,才显示,小于默认忽略
            //            10001 10001 % 10000 = 1 < 10000 / 10 直接除
            if (count % 10000) < 1000{
                return "\(count / 10000)万"
            }
            return  "\(Double(count) / 10000)万"
        }else{
            return defaultTitle
        }
    }



    func  clearMbRank(){

        let mbRank = statusList!.user?.mbrank ?? 0

        if mbRank > 0 && mbRank < 7 {
            mbImage = UIImage(named: "common_icon_membership_level\(mbRank)")
        }
    }


    /**
     用户认证类型处理
     */
    func clearVerifiedImage(){
        //认证类型
        /// 认证类型 -1：没有认证，1：认证用户(avatar_grassroot)，2,3,5: 企业认证(avatar_enterprise_vip)，220: 达人(avatar_vip)
        let verifiedType = statusList!.user?.verified_type ?? -1

        switch verifiedType{
        case 1:
            verifiedImage = UIImage(named:"avatar_grassroot" )
        case 2,3,5:
            verifiedImage = UIImage(named:"avatar_enterprise_vip" )
        case 220:
            verifiedImage = UIImage(named:"avatar_vip" )
        default:
            verifiedImage = nil
        }
    }
}
